<? 
		if(isset($_POST['btnsubmit']))
		{
			require("required/dbconnect.php");
			require("required/function.php"); 
			checkAccess("allow_product_edit",1); 
			
			$cat_id=$_POST['cat_id'];
			$sub_cat=$_POST['sub_cat'];
	
			$product_name_eng=$_POST['product_name_eng'];
			$display_name=$_POST['display_name'];
			$product_code=$_POST['product_code'];
			$product_name_eng_current=$_POST['product_name_eng_current'];
			$product_code_current=$_POST['product_code_current'];
			$promotion=$_POST['promotion'];
			$featured=$_POST['featured'];
			$status=$_POST['status'];
			$youtube_id=$_POST['youtube_id'];
			$product_weight=$_POST['product_weight'];
			$promotion=$_POST['promotion'];
			$promotion_start=$_POST['promotion_start'];
			$promotion_end=$_POST['promotion_end'];
			$promotion_target=$_POST['promotion_target'];
			$stock_option_type=$_POST['stock_option_type'];
			$show_stock_quantity=$_POST['show_stock_quantity'];
			$show_stock_option=$_POST['show_stock_option'];
			$description_eng=$_POST['description_eng'];
			$show_at_latest=$_POST['show_at_latest'];
			
			$remark_eng=$_POST['remark_eng'];
			$id=$_POST['hidid'];
			
			$order_id=$_POST['order_id'];
			$redeemable=$_POST['redeemable'];
			$hide_at_products=$_POST['hide_at_products'];
			$points_required=$_POST['points_required'];
			$clean_url=cleanURL($product_name_eng);
			$error=0;
			
			if($product_code!=$product_code_current)
			{
				$chk=mysql_query("select count(*) as result from os_products where product_code='$product_code'")or die(mysql_error());
				
				if(mysql_result($chk,0,"result")>0)
				{
					$error=1;
				}
			}
			
			if($product_name_eng!=$product_name_eng_current)
			{
				$chk=mysql_query("select count(*) as result from os_products where product_name_eng='$product_name_eng'")or die(mysql_error());
				
				if(mysql_result($chk,0,"result")>0)
				{
					$error=2;
				}
			}
			
			if($error!=0)
			{
				header("Location:product_edit.php?id=$id&error=$error");
			}
			else
			{
				$insert=mysql_query("update os_products set  cat_id=$cat_id,sub_cat_id='$sub_cat',product_name_eng='$product_name_eng',display_name='$display_name',
				product_code='$product_code',clean_url='$clean_url',product_weight='$product_weight',promotion='$promotion',promotion_start='$promotion_start',
				promotion_end='$promotion_end',promotion_target='$promotion_target',youtube_id='$youtube_id',order_id='$order_id',featured=$featured,remark_eng='$remark_eng',
				stock_option_type='$stock_option_type',show_stock_option='$show_stock_option',show_stock_quantity='$show_stock_quantity',description_eng='$description_eng',
				show_at_latest='$show_at_latest', status=$status,redeemable='$redeemable',hide_at_products='$hide_at_products',points_required='$points_required' where id=$id")or die(mysql_error());
			
				header("Location:product_viewfull.php?id=$id&updated=1");
				
			}
		}
		else
		{

				require("required/dbconnect.php"); 
				require("required/function.php"); 
				require("required/header.php"); 
				
				
				$id=$_GET['id'];
				$get_id=mysql_query("select * from os_products where id=$id");
			
				$a=mysql_fetch_array($get_id,MYSQL_ASSOC);
				
				if($a['status']==0)
				{
					$slt0="selected";
					$slt1="";
					
				}
				else
				{
					$slt0="";
					$slt1="selected";
				}
				
				if($a['promotion']==0)
				{
					$pslt0="selected";
					$pslt1="";
					
				}
				else
				{
					$pslt0="";
					$pslt1="selected";
				}
				
				if($a['show_at_latest']==0)
				{
					$lslt0="selected";
					$lslt1="";
					
				}
				else
				{
					$lslt0="";
					$lslt1="selected";
				}
				
				if($a['redeemable']==0)
				{
					$rdslt0="selected";
					$rdslt1="";
					
				}
				else
				{
					$rdslt0="";
					$rdslt1="selected";
				}
				
				if($a['hide_at_products']==0)
				{
					$sapslt0="selected";
					$sapslt1="";
					
				}
				else
				{
					$sapslt0="";
					$sapslt1="selected";
				}
				
				if($a['show_at_latest']==0)
				{
					$lslt0="selected";
					$lslt1="";
					
				}
				else
				{
					$lslt0="";
					$lslt1="selected";
				}
				
				if($a['featured']==0)
				{
					$fslt0="selected";
					$fslt1="";
					
				}
				else
				{
					$fslt0="";
					$fslt1="selected";
				}
				
				if($a['show_stock_quantity']==0)
				{
					$qslt0="selected";
					$qslt1="";
					
				}
				else
				{
					$qslt0="";
					$qslt1="selected";
				}
				
				if($a['show_stock_option']==0)
				{
					$qoslt0="selected";
					$qoslt1="";
					
				}
				else
				{
					$qoslt0="";
					$qoslt1="selected";
				}
				
				switch($a['product_type'])
				{
					case "product":
						checkAccess("allow_product_edit",1);
						$title="Edit Product";
						break;
					case "gift";
						checkAccess("allow_gift_management",1);
						$title="Edit Gift";
						break;
					default:
						checkAccess("allow_product_add",1);
						$title="Edit Product";
						break;
				}
				

?>

<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<?
	
	checkAccess("allow_product_management",1);
	
	if(!empty($_GET['error'])){
		$error=$_GET['error'];
		
		switch($error){
			case 1:
				$msg="<div class='dw_failed'>Product code already exist, update failed</div>";
				break;
			case 2:
				$msg="<div class='dw_failed'>Product name already exist, update failed</div>";
				break;
			case 4:
				$msg="<div class='dw_success'>Image Deleted</div>";
				break;	
			default:
				$msg="unknown";
				break;
		}
		echo "<script>displayMsg('divMsg')</script>";	
	}else{
		$msg="";
	}
?>

<script language="javascript">
	
	function getSubCat(catId) 
	{		
		changeContent("required/js/getsubcat.php?catid="+catId,"divSubCat")
	}

	function validatefrm(frm)
	{
		if(document.frm.cat_id.value=="")
		{
			alert("Please select main category");
			document.frm.cat_id.focus();
			return false;
		}
		if(document.frm.sub_cat.value=="")
		{
			alert("Please select sub category");
			document.frm.sub_cat.focus();
			return false;
		}
		if(document.frm.product_name_chn.value=="")
		{
			alert("Please enter product name(chinese)");
			document.frm.product_name_chn.focus();
			return false;
		}
		if(document.frm.product_name_eng.value=="")
		{
			alert("Please enter product name(english)");
			document.frm.product_name_eng.focus();
			return false;
		}
		if(document.frm.product_code.value=="")
		{
			alert("Please enter product code");
			document.frm.product_code.focus();
			return false;
		}
		return true;
	
	}
</script>
				<table width="100%"  cellpadding="0" cellspacing="5" height="300">
				
				<script language="JavaScript" type="text/javascript" src="required/js/goAjax.js"></script>
                <script type="text/javascript" src="required/js/calendarDateInput.js"></script>
                 <SCRIPT language="JavaScript" src="required/js/tipmessage.js" type="text/javascript"></SCRIPT>
				<SCRIPT language="JavaScript" src="required/js/tipmessage_style.js" type="text/javascript"></SCRIPT>
				<DIV id="TipLayer" style="visibility:hidden;position:absolute;z-index:1000;top:-100"></DIV>		
					<td width="84%" align="left" valign="top">
						<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" onSubmit="return validatefrm(this)">
						<table width="100%">
                       
                        <tr>
						<td class="title" colspan="2"><?= $title ?></td>
                        </tr>
                         <tr>
							<td colspan="3"> <div id="divMsg"><?= $msg ?></div></td>
						</tr>
						<tr>
                        	<td colspan="2" class="sub_title_line" ><br />Classification </td>
                        </tr>
						<?
							$get_cat=mysql_query("select * from os_category order by id asc");
						?>
						
                        <tr>
							<Td width="13%">Category</Td>
							<td width="87%">
								<select name="cat_id" class="text" onChange="getSubCat(this.value)">
								<?
									while($c=mysql_fetch_array($get_cat,MYSQL_ASSOC))
									{
										if($c['id']==$a['cat_id'])
										{
											$cslt="selected";
										}
										else
										{
											$cslt="";
										}
								?>
									<option value="<?= $c['id'] ?>" <?= $cslt ?>><?= $c['name_eng'] ?></option>
								<? } ?>
								</select>
								
							</td>
						</tr>
						<?
							$get_subcat=mysql_query("select * from os_sub_category where main_category=".$a['cat_id']);
						
						?>
						<tr>
							<td>Sub Category</td><td>
							<div id="divSubCat">
								<select name="sub_cat" class="text">
								<?
									while($s=mysql_fetch_array($get_subcat,MYSQL_ASSOC))
									{
										if($s['id']==$a['sub_cat_id'])
										{
											$sslt="selected";
										}
										else
										{
											$sslt="";
										}
								?>
									<option value="<?= $s['id'] ?>" <?= $sslt ?>><?= $s['name_eng'] ?></option>
								<? } ?>
								</select>					
							</div>
							</td>
						</tr>
                        
						<tr>
							<td>Show at Website</td>
							<td>
								<select name="status" class="text">
									<option value="0" <?= $slt0 ?>>No</option>
									<option value="1" <?= $slt1 ?>>Yes</option>
								</select>	
							</td>
						</tr>
                       <tr>
							<td>Show at Latest</td>
							<td>
								<select name="show_at_latest" class="text">
									<option value="0" <?= $lslt0 ?>>No</option>
									<option value="1" <?= $lslt1 ?>>Yes</option>
								</select>	
							</td>
						</tr>
                       <tr>
							<td>Is Featured</td>
							<td>
								<select name="featured" class="text">
									<option value="0" <?= $fslt0 ?>>No</option>
									<option value="1" <?= $fslt1 ?>>Yes</option>
								</select>	
							</td>
						</tr>
                       
                        <tr>
                        	<td colspan="2" class="sub_title_line" ><br />Product Details </td>
                        </tr>
						<tr>
							<td>Product name</td><td><input name="product_name_eng" size="50" maxlength="200" class="text" value="<?= htmlspecialchars( $a['product_name_eng'] ) ?>">
                            <input type="hidden" name="product_name_eng_current" value="<?= htmlspecialchars( $a['product_name_eng'] ) ?>">
                            </td>
						</tr>
                        <tr>
							<td>Product name</td><td><input name="display_name" size="50" maxlength="200" class="text" value="<?= htmlspecialchars( $a['display_name'] ) ?>">
                            
                            </td>
						</tr>
                         <tr>
							<td>Product weight</td><td><input name="product_weight" size="3" maxlength="5" value="<?= $a['product_weight'] ?>" class="text"> KG</td>
						</tr>
						<tr>
							<td>Product code</td>
                            <td><input name="product_code" size="20" maxlength="20" class="text" value="<?= htmlspecialchars( $a['product_code'] ) ?>">
                            <input type='hidden' name="product_code_current" value="<?= htmlspecialchars( $a['product_code'] ) ?>">
                            </td>
						</tr>
                        <tr valign="top">
							<td>Order ID</td><td><input name="order_id" size="3" maxlength="5" value="<?= htmlspecialchars( $a['order_id'] ) ?>">
							
						
							</td>
						</tr>
                        <input type="hidden" name="remark_eng" value="<?= $a['remark_eng'] ?>" />
						
                        <tr valign="top">
							<td>Youtube Video ID</td><td><input name="youtube_id" size="50" maxlength="20" class="text" value="<?= $a['youtube_id'] ?>">
                            <br />
                            
                            e.g : http://www.youtube.com/v/<b>sdUUx5FdySs</b> 
                            <br />where <strong>sdUUx5FdySs</strong> is the Video Id
                            <br />
                            Leave it blank if no video included.
                            </td>
						</tr>
                       
						<tr><td><br></td></tr>
						<tr valign="top">
							<td>Description</td>
							<td valign="top">
								<? include_once("fckeditor/fckeditor.php") ?>
								<?php
									$oFCKeditor = new FCKeditor('description_eng') ;
									$oFCKeditor->ToolbarSet = 'DeswinBasic';
									$oFCKeditor->BasePath = './fckeditor/' ;
									$oFCKeditor->Value = $a['description_eng'] ;
									$oFCKeditor->Create() ;
								?>
								
							</td>
						</tr>
                         <tr>
                        	<td colspan="2" class="sub_title_line" ><br />Stock</td>
                        </tr>
						<tr>
                        	<td>Option Type</td><td><input type="text" size="20" maxlength="20" name="stock_option_type" value="<?= $a['stock_option_type'] ?>" /> 
                            <A href="#" onMouseOver="stm(Text[0],Style[0])" onMouseOut="htm()" style="text-decoration:none"> <font class="what_is_this">what is this?</font></A></td>
                        </tr>
						<tr>
                        	<td>Show Option Type</td><td>
                            <select name="show_stock_option" /> 
                            	
                                	<option value="1" <?= $qoslt1 ?>>Yes</option>
									<option value="0" <?= $qoslt0 ?>>No</option>
								
                            </select>
                            <A href="#" onMouseOver="stm(Text[2],Style[0])" onMouseOut="htm()" style="text-decoration:none"> <font class="what_is_this">what is this?</font></A></td>
                        </tr>		
                        <tr>
                        	<td>Show Available Quantity</td><td>
                            <select name="show_stock_quantity" /> 
                            	
                                	<option value="1" <?= $qslt1 ?>>Yes</option>
									<option value="0" <?= $qslt0 ?>>No</option>
								
                            </select>
                            <A href="#" onMouseOver="stm(Text[1],Style[0])" onMouseOut="htm()" style="text-decoration:none"> <font class="what_is_this">what is this?</font></A></td>
                        </tr>					
                        <?
							if($module_promotion==1)
							{
						?>
						 <tr>
                        	<td colspan="2" class="sub_title_line" ><br />Promotion </td>
                        </tr>
						<tr>
							<td>Promotion</td>
							<td>
                            	<select name="promotion">
                            		<option value="0" <?= $pslt0 ?>>No</option>
                                    <option value="1" <?= $pslt1 ?>>Yes</option>
                                </select>
                            </td>
						</tr>
                        	
                        <tr valign="top">
                            	<td width="264">Promotion Start</td>
		 	  	 		 <td width="1341">
									<script>var start_date="<?= $a['promotion_start'] ?>" ;
									DateInput('promotion_start', true, 'YYYY-MM-DD',start_date)</script>
                                </td>
                        </tr>
                        <tr valign="top">
                            <td width="264">Promotion End</td>
                          <td width="1341">
                           <script>var start_date="<?= $a['promotion_end'] ?>" ;
									DateInput('promotion_end', true, 'YYYY-MM-DD',start_date)</script>
                          </td>
                        </tr>
                        <? }else{ ?>
                         <input type="hidden" name="promotion_start" value="0" />
                        <input type="hidden" name="promotion_end" value="0" />
                        <input type="hidden" name="promotion" value="0" />
                        
                        <? } ?>
                        <?
							if($a['promotion_target']=="N")
							{
								$ptsltN="selected";
								$ptsltV="";
								$ptsltB="";
								
							}
							elseif($a['promotion_target']=="V")
							{
								$ptsltN="";
								$ptsltV="selected";
								$ptsltB="";
								
							}
							else
							{
								$ptsltN="";
								$ptsltV="";
								$ptsltB="selected";
							}
						?>
                        <?
							if($module_special_member==1)
							{
						?>
                        <tr valign="top">
                            <td width="264">Promotion Open to</td>
                            <td width="1341">
                          	<select name="promotion_target">
                              <option value="V" <?= $ptsltV ?>>VIP Members</option>
                              <option value="B" <?= $ptsltB ?>>Both VIP and Normal Members</option>
                             </select>
                          </td>
                        </tr>
                        <? }else{ ?>
                        <input type="hidden" name="promotion_target" value="B" />
                        <? }} ?>
						
                        <?
							if($module_redeemable==1)
							{
						?>
                         <tr>
                        	<td colspan="2" class="sub_title_line" ><br />Redeemable Product</td>
                        </tr>
                        <tr>
							<td>Redeemable</td>
							<td>
								<select name="redeemable" class="text">
									<option value="0" <?= $rdslt0 ?>>No</option>
									<option value="1" <?= $rdslt1 ?>>Yes</option>
								</select>	
							</td>
						</tr>
                         <tr>
							<td>Do Not Show at Product Page</td>
							<td>
								<select name="hide_at_products" class="text">
                                	<option value="1" <?= $sapslt1 ?>>Yes</option>
									<option value="0" <?= $sapslt0 ?>>No</option>
								</select>	
							</td>
						</tr>
                        <tr>
							<td>Points Required</td><td><input name="points_required" size="20" maxlength="6" class="text" value="<?= $a['points_required'] ?>"></td>
						</tr>
                        <? }else{ ?>
                        <input type="hidden" name="redeemable" value="0">
						<input type="hidden" name="hide_at_products" value="0">
						<input type="hidden" name="points_required" value="0">
                        <? } ?>
						
						
						<input type="hidden" name="actual_price" value="0">
						
						<? /*
						<tr>
							<td>Promotion</td>
							<td>
								<select name="promotion" class="text">
									<option value="0" <?= $pslt0 ?>>No</option>
									<option value="1" <?= $pslt1 ?>>Yes</option>
								</select>	
							</td>
						</tr>
						<tr>
							<td></td>
							<td >
							<table class="solidborder">
								<tr>
									<td colspan="2"><strong>FOR PROMOTION ITEMS ONLY</strong></td>
								</tr>
								<tr>
									<td width="136">Actual Price</td><td width="271"><input name="actual_price" size="10" maxlength="10" class="text" value="<?= $a['actual_price'] ?>"></td>
								</tr>
								<tr valign="top">
									<td>Order ID*

									</td><td><input name="order_id" size="10" maxlength="5" class="text" value="<?= $a['order_id'] ?>"></td>
								</tr>
								<tr>
									<td colspan="2">* The item will arrange according to order ID descendingly</td>
								</tr>
							</table>
						</td>
						</tr>
						<tr>
							<td>Featured</td>
							<td>
								<select name="featured" class="text">
									<option value="0" <?= $fslt0 ?>>No</option>
									<option value="1" <?= $fslt1 ?>>Yes</option>
								</select>	
							</td>
						</tr>
						*/ ?>
						
						
						
						<input type="hidden" value="<?= $id ?>" name="hidid">
						<tr>
							<td colspan="2"><input type="submit" class="button" value="Submit" name="btnsubmit"> <input type="button" onClick="history.go(-1)" class="button" value="Back"></td>
						</tr>
						</table>
						</form>
					</td>
				 
				</table>
<? require("required/footer.php"); ?>